package com.fullstack.landscape.editor.controller;

import cn.hutool.http.HtmlUtil;
import com.fullstack.landscape.editor.common.BizType;
import com.fullstack.landscape.editor.common.Result;
import com.fullstack.landscape.editor.controller.vo.SpotListVO;
import com.fullstack.landscape.editor.service.SpotService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

import static org.slf4j.LoggerFactory.getLogger;

@RestController
@RequestMapping("/api/spot")
public class SpotController {

    private final SpotService spotService;

    /**
     * logger
     */
    private static final Logger log = getLogger(SpotController.class);


    public SpotController(SpotService spotService) {
        this.spotService = spotService;
    }

    @GetMapping("/{moduleId}/list")
    public Result<List<SpotListVO>> getSpotList(@PathVariable int moduleId) {
        List<SpotListVO> spotList = spotService.getSpotList(moduleId);
        return Result.success(spotList);
    }

    @PostMapping("/{moduleId}/save")
    public Result<Void> save(@PathVariable int moduleId,
                             @RequestParam("name") String name,
                             @RequestParam(value = "avatarPath", required = false) String avatarPath) {
        spotService.save(moduleId, name, avatarPath);
        return Result.success();
    }

    @PostMapping("/delete/{id}")
    public Result<Void> delete(@PathVariable int id) {
        spotService.delete(id);
        return Result.success();
    }

    /**
     * 更新名称
     *
     * @return success
     */
    @PostMapping("/updateBasicInfo/{id}")
    public Result<Void> updateBasicInfo(@PathVariable int id,
                                        @RequestParam("name") String name,
                                        @RequestParam(value = "avatarPath", required = false) String avatarPath) {
        spotService.updateBasicInfo(id, name, avatarPath);
        return Result.success();
    }

    /**
     * 上传视频
     *
     * @return success
     */
    @PostMapping("/setVideo/{id}")
    public Result<Void> uploadVideo(@PathVariable int id, @RequestParam(value = "videoPath", required = false) String videoPath) {
        spotService.uploadVideo(id, videoPath);
        return Result.success();
    }

    /**
     * 保存内容
     *
     * @return success
     */
    @PostMapping("/saveContent/{id}")
    public Result<Void> saveContent(@PathVariable int id, @RequestBody(required = false) String content) throws UnsupportedEncodingException {
        spotService.saveContent(id, content);
        return Result.success();
    }

    /**
     * 上传图片
     *
     * @return success
     */
    @PostMapping("/uploadAttachment")
    public Result<String> uploadAttachment(@RequestParam(value = "file", required = false) MultipartFile file) {
        String imageUrl = spotService.uploadImage(1, file);
        return Result.success(imageUrl);
    }


    @PostMapping("/removeVideo/{id}")
    public Result<Void> removeVideo(@PathVariable int id) {
        spotService.removeVideo(id, BizType.SPOT);
        return Result.success();
    }


}
